package com.rockcent.service.impl;

import com.rockcent.common.common.PageResult;
import com.rockcent.common.utils.ExcelCreateUtils;
import com.rockcent.mall.lib.domain.MallMemberFundFlow;
import com.rockcent.mall.lib.domain.MallMemberRechargeFlow;
import com.rockcent.mall.lib.domain.enumclass.MallMemberFundTransactionType;
import com.rockcent.mall.lib.domain.enumclass.TransactionType;
import com.rockcent.mall.lib.dto.MallMemberFundFlowDto;
import com.rockcent.mall.lib.repository.MallMemberFundFlowRepository;
import com.rockcent.service.MallMemberFundFlowSrv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangjh on 2017/3/9.
 */
@Service
public class MallMemberFundFlowSrvImpl implements MallMemberFundFlowSrv {
    List names = new ArrayList();

    {
        names.add("序号");
        names.add("时间");
        names.add("交易类型");
        names.add("流水号");
        names.add("金额");
        names.add("账户余额");
    }

    @Autowired
    private MallMemberFundFlowRepository mallMemberFundFlowRepository;


    @Override
    public PageResult<MallMemberFundFlowDto> findMallMemberFundFlowPage(MallMemberFundFlowDto dto, PageResult page) {
        return mallMemberFundFlowRepository.findMallMemberFundFlowPage(dto, page);
    }

    @Override
    public void export(HttpServletRequest request, HttpServletResponse response, MallMemberFundFlowDto dto) throws IOException {
        PageResult page = new PageResult(0, Integer.MAX_VALUE);
        PageResult<MallMemberFundFlowDto> pageResult = findMallMemberFundFlowPage(dto, page);
//        Long total = 0l;
        List<MallMemberFundFlowDto> items = pageResult.data;
        if (null == items || items.isEmpty()) {
            items = new ArrayList<>();
        }

        for (MallMemberFundFlowDto memberFundFlowDto : items) {
            String transactionType = MallMemberFundTransactionType.valueOf(memberFundFlowDto.getTransactionType()).getValue();
            memberFundFlowDto.setTransactionType(transactionType);
            memberFundFlowDto.setEffectiveDate(memberFundFlowDto.getLastUpdated().toString());
        }

//        total = Long.valueOf(items.size());
        Map<String, Object> data = new HashMap<>();
        data.put("data", items);
        data.put("headers", names);
        String titles = "id,effectiveDate,transactionType,seqNo,operationAmount,balance";
        String fileName = "会员账户资金记录报表";
        String dataName = "data";

        ExcelCreateUtils.getInstance().createDynamic(data, dataName, fileName + SUFFIX_XLSX, response, RESOURCE_PATH_MALL_MEMBER_FUND_DETAIL_DYNAMIC, titles);


    }

    @Override
    public MallMemberFundFlow save(MallMemberFundFlow fundFlow) {
        return mallMemberFundFlowRepository.save(fundFlow);
    }
}
